package com.meng.menggaga.ui;

import uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.AbcDefaultHeaderTransformer;
import uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.Options;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.meng.menggaga.R;
import com.meng.menggaga.adapter.CommunityBannerAdapter;
import com.meng.menggaga.adapter.CommunityListAdapter;
import com.meng.menggaga.application.MainApplication;
import com.meng.menggaga.entity.CommunityInfos;
import com.meng.menggaga.entity.CommunityInfos.CommunityInfo;
import com.meng.menggaga.util.CommonParameter;
import com.meng.menggaga.util.Contants;
import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingRightInAnimationAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;


/**
 * 社区详情
 * 
 * 首页列表进入的位置，用户发表的内容，及用户评论的地方
 * 
 * @author chenghao
 * 
 */
public class CommunityDetailActivity extends BaseActivity implements OnRefreshListener {

    private ListView vListView;
    private PullToRefreshLayout mPullToRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setActionBarTitle(R.string.detail);

        vListView = (ListView) findViewById(R.id.listview);
        // We can now setup the PullToRefreshLayout

        mPullToRefreshLayout = (PullToRefreshLayout) findViewById(R.id.ptr_layout);
        AbcDefaultHeaderTransformer transformer = new AbcDefaultHeaderTransformer();
        ActionBarPullToRefresh.from(this)
        // We need to insert the PullToRefreshLayout into the Fragment's ViewGroup
        // .insertLayoutInto((ViewGroup) getWindow().getDecorView())
                .options(Options.create().headerTransformer(transformer).build())
                // Here we mark just the ListView and it's Empty View as pullable
                .theseChildrenArePullable(R.id.listview).listener(this).setup(mPullToRefreshLayout);

        // transformer.getHeaderView().setBackgroundColor(
        // getResources().getColor(CommonParameter.sStyleBgColor));
        transformer.setProgressBarColor(getResources().getColor(CommonParameter.sStyleBgColor));
        transformer.setProgressBarBgColor(getResources().getColor(CommonParameter.sStyleBgColor));
        transformer
                .setHeaderTextViewBgColor(getResources().getColor(CommonParameter.sStyleBgColor));
        vListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // startActivity(new Intent(getBaseContext(), DemoActivity.class));
            }
        });

        vListView.addHeaderView(getHeaderView((CommunityInfo) getIntent().getSerializableExtra(
                Contants.KEY_COMMUNITY_INFO)));

    }

    private View getHeaderView(CommunityInfo info) {
        View view = View.inflate(this, R.layout.list_item_community_detail_landlord, null);
        ((TextView) view.findViewById(R.id.content)).setText(info.subject);
        ImageLoader.getInstance().displayImage(info.author,
                ((ImageView) view.findViewById(R.id.icon)));
        return view;
    }

    @Override
    protected void onLoadDatas() {
        // 获取楼主信息
        MainApplication.get().getMoccaApi()
                .getCommunityList(42, 1, 10, new Listener<CommunityInfos>() {

                    @Override
                    public void onResponse(CommunityInfos response) {
                        if (null != response) {
                            View headerView =
                                    View.inflate(getBaseContext(),
                                            R.layout.layout_community_banner, null);
                            ViewPager vViewPager =
                                    (ViewPager) headerView.findViewById(R.id.viewpager);

                            vViewPager.setAdapter(new CommunityBannerAdapter(getBaseContext(),
                                    response.array));
                            try {
                                vViewPager.setPageTransformer(true,
                                        Contants.TRANSFORM_CLASSES.get(1).clazz.newInstance());
                            } catch (java.lang.InstantiationException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }

                            vListView.addHeaderView(headerView);
                            loadCommunitys();
                        }
                    }
                }, new ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {}
                });

        MainApplication.get().getMoccaApi()
                .getCommunityList(42, 1, 10, new Listener<CommunityInfos>() {

                    @Override
                    public void onResponse(CommunityInfos response) {
                        if (null != response) {
                            View headerView =
                                    View.inflate(getBaseContext(),
                                            R.layout.layout_community_banner, null);
                            ViewPager vViewPager =
                                    (ViewPager) headerView.findViewById(R.id.viewpager);

                            vViewPager.setAdapter(new CommunityBannerAdapter(getBaseContext(),
                                    response.array));
                            try {
                                vViewPager.setPageTransformer(true,
                                        Contants.TRANSFORM_CLASSES.get(1).clazz.newInstance());
                            } catch (java.lang.InstantiationException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }

                            vListView.addHeaderView(headerView);
                            loadCommunitys();
                        }
                    }
                }, new ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {}
                });
    }

    private void loadCommunitys() {// getArguments().getInt(ARG_SECTION_NUMBER)
        MainApplication.get().getMoccaApi()
                .getCommunityList(37, 1, 10, new Listener<CommunityInfos>() {

                    @Override
                    public void onResponse(CommunityInfos response) {
                        if (null != getBaseContext()) {
                            if (null != response) {
                                CommunityListAdapter adapter =
                                        new CommunityListAdapter(getBaseContext(), response.array,
                                                10, R.layout.list_item_community_list,
                                                R.layout.list_item_loading,
                                                R.layout.list_item_retry);
                                AnimationAdapter animAdapter =
                                        new SwingRightInAnimationAdapter(adapter);
                                animAdapter.setAnimationDurationMillis(3000);
                                animAdapter.setAbsListView(vListView);

                                vListView.setAdapter(animAdapter);

                                mPullToRefreshLayout.setRefreshComplete();
                            }
                        }
                    }
                }, new ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {}
                });
    }

    @Override
    public void onRefreshStarted(View view) {
        // Notify PullToRefreshLayout that the refresh has finished
        loadCommunitys();
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_community_detail;
    }
}
